package com.imooc.distributedemo.resume.leetcode;

/**
 * leetcode35 搜索插入位置
 */
public class Solution35 {
    public static void main(String[] args) {
//        int[] nums={1,3,5,6};
//        int k = 5;
    int[] nums={1,3,5,6};
    int k = 2;
        int result = searchInsert(nums,k);
        System.out.println(result);
    }
    public static  int searchInsert(int[] nums, int target) {
        if(null==nums||nums.length<1){
            return 0;
        }
        if(target<=nums[0]){
            return 0;
        }
        if(target>=nums[nums.length-1]){
            return nums.length-1;
        }
        int left=0,right=nums.length-1;

        while (left<=right){
            int mid = left + (right-left)/2;
            if(target>nums[mid]){
                left = mid+1;
            }else{
                right = mid - 1;
            }
        }
        return left;
    }
}


